// experience-form.js
Page({
    data: {
        options: {},
        formData: {
            startDate: '',
            endDate: '',
            companyName: '',
            employee: '',
            workContent: '' // 添加工作内容字段
        },
    },
    onLoad: function (options) {
        this.setData({
            options: options // 将 options 存储在 data 中
        });
        if (options && options.index) {
            wx.getStorage({
                key: 'experienceList',
                success: (res) => {
                    const { data } = res;
                    const experience = data[options.index];
                    this.setData({
                        formData: {
                            startDate: experience.startDate,
                            endDate: experience.endDate,
                            companyName: experience.companyName,
                            employee: experience.employee,
                            workContent: experience.workContent || '' // 确保有默认值
                        }
                    });
                },
                fail: (err) => {
                    console.log('获取个人表单数据失败', err);
                }
            });
        }
    },
    startChange: function (e) {
        this.setData({
            'formData.startDate': e.detail.value
        });
    },
    endChange: function (e) {
        this.setData({
            'formData.endDate': e.detail.value
        });
    },
    companyNameChange: function (e) {
        this.setData({
            'formData.companyName': e.detail.value
        });
    },
    employeeChange: function (e) {
        this.setData({
            'formData.employee': e.detail.value
        });
    },
    workContentChange: function (e) {
        this.setData({
            'formData.workContent': e.detail.value
        });
    },
    submitForm: function (e) {
        const experienceList = wx.getStorageSync('experienceList') || [];
        if (this.data.options && this.data.options.index) {
            experienceList[this.data.options.index] = this.data.formData;
        } else {
            experienceList.push(this.data.formData);
        }
        wx.setStorageSync('experienceList', experienceList);
        wx.navigateBack();
    }
});
